package xingkong.core;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import com.alibaba.druid.util.StringUtils;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import xingkong.tools.ResponseContent;
import xingkong.tools.StaticFields;
/**
 * 业务
 * @author befory
 *
 */
public class BusinessController extends Controller{
	/**
	 * 去设置后台
	 */
	public void toSetAdmin(){
		renderJsp("/WEB-INF/content/setAdmin.jsp");
	}
	
	/**
	 * 添加业务
	 * 管理员操作
	 */
	public void addBusiness(){
		Record user = (Record) getSession().getAttribute(StaticFields.CURRENTUSER);
		if(!"1".equals(user.get("type"))){
			renderJson(new ResponseContent(StaticFields.WRONG, "只有管理员可以执行此操作", null));
			return;
		}
		String name = getPara("name");//名称
		if(StringUtils.isEmpty(name)){
			renderJson(new ResponseContent(StaticFields.WRONG, "名称不能为空", null));
			return;
		}
		int type = getParaToInt("type");//类型
		if(type < 1){
			renderJson(new ResponseContent(StaticFields.WRONG, "请选择正确的类型", null));
			return;
		}
		int price = getParaToInt("price");//价格
		if(price<0){
			renderJson(new ResponseContent(StaticFields.WRONG, "请输入正确的价格", null));
			return;
		}
		Record record = this.checkCurrentType(type);
		if(null == record){
			boolean f = Db.save("tb_business", new Record().set("name", name).set("type", type).set("price", price).set("create_time", new Date()));
			if(!f){
				renderJson(new ResponseContent(StaticFields.WRONG, "添加失败", null));
				return;
			}
		}else {
			int i = Db.update("update tb_business set price = ? where id=?",price,record.get("id"));
			if(i<=0){
				renderJson(new ResponseContent(StaticFields.WRONG, "添加失败", null));
				return;
			}
		}
		renderJson(new ResponseContent(StaticFields.CORRECT, "添加成功", null));
	}
	/**
	 * 获取字典表中业务类型的总数
	 * @return
	 */
//	public long getBusinessCount(){
//		return Db.queryLong("select count(1) from tb_dictionary where dictionary_type = 1");
//	}
	/**
	 * 添加字典表
	 */
	public void addDictionary(){
		Record user = (Record) getSession().getAttribute(StaticFields.CURRENTUSER);
		if(!"0".equals(user.get("type"))){
			renderJson(new ResponseContent(StaticFields.WRONG, "只有管理员可以执行此操作", null));
			return;
		}
//		String dictionary_type = getPara("dictionary_type");
//		if(StringUtils.isEmpty(dictionary_type)){
//			renderJson(new ResponseContent(StaticFields.WRONG, "字典类型不能为空", null));
//			return;
//		}
		String dictionary_value = getPara("dictionary_value");
		if(StringUtils.isEmpty(dictionary_value)){
			renderJson(new ResponseContent(StaticFields.WRONG, "字典值不能为空", null));
			return;
		}
		boolean f = Db.save("tb_dictionary", new Record().set("dictionary_type", "1").set("dictionary_value", dictionary_value).set("create_time", new Date()));
		if(!f){
			renderJson(new ResponseContent(StaticFields.WRONG, "添加失败", null));
			return;
		}
		renderJson(new ResponseContent(StaticFields.CORRECT, "添加成功", null));
	}
	
	/**
	 * 修改默认注册积分
	 */
	public void updateDictionary(){
		Record user = (Record) getSession().getAttribute(StaticFields.CURRENTUSER);
		if(!"1".equals(user.get("type"))){
			renderJson(new ResponseContent(StaticFields.WRONG, "只有管理员可以执行此操作", null));
			return;
		}
		String dictionary_value = getPara("dictionary_value");
		if(StringUtils.isEmpty(dictionary_value)){
			renderJson(new ResponseContent(StaticFields.WRONG, "积分值不能为空", null));
			return;
		}
		String reg = "^[1-9]+$";
		if(!Pattern.matches(reg, dictionary_value)){
			renderJson(new ResponseContent(StaticFields.WRONG, "积分值必须为正整数", null));
			return;
		}
		int i = Db.update("update tb_dictionary set dictionary_value = ? where dictionary_type = '2'",dictionary_value);
		if (i<1) {
			renderJson(new ResponseContent(StaticFields.WRONG, "修改失败", null));
			return;
		}
		renderJson(new ResponseContent(StaticFields.CORRECT, "修改成功", null));
	}
	/**
	 * 获取业务集合
	 */
	public void getBusiness(){
		List<Record> list = Db.find("select * from tb_business b");
		renderJson(new ResponseContent(StaticFields.CORRECT, "", list));
	}
	
	/**
	 * 检查当前类型的业务是否存在
	 * @param type 类型
	 * @return true 存在 false 不存在
	 */
	private Record checkCurrentType(int type){
		return Db.findFirst("select id,price,name,type from tb_business where type = ?",type);
	}
	
//	public static void main(String[] args) {
//		String reg = "^[1-9]+$";
//		System.out.println(Pattern.matches(reg, "012"));
//	}
}
